// 登陆注册模块
import { reqGetCode, reqUserRegister, reqUserLogin, reqUserInfo } from "@/api"
const state = {
    code: "",
    data: {},
    token:'',
    userInfo: {}
}
const mutations = {
    GATCODE(state, code) {
        state.code = code
    },
    REGISTER(state, data) {
        state.data = data
    },
    LOGIN(state, token) {
        state.token = token
    },
    USERINFO(state, userInfo) {
        state.userInfo = userInfo
    }
}
const actions = {
    // 获取验证码
    async getCode({commit}, phone) {
        let result = await reqGetCode(phone)
        if(result.code==200) {
            commit("GATCODE",result.data)
            // console.log(result)
            return 'ok'
        }else{
            return Promise.reject(new Error('false'))
        }
    },
    // 用户注册
    async userRegister({ commit }, user) {
        let result = await reqUserRegister(user)
        console.log(result)
        if(result.code==200) {
            commit("REGISTER", result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error('false'))
        }
    },
    // 用户登录
    async userLogin({ commit }, data) {
        let result = await reqUserLogin(data)
        // console.log(result)
        if(result.code==200) {
            commit("LOGIN", result.data.token)
            return 'ok'
        }else{
            return Promise.reject(new Error('false'))
        }
    },
    // 获取用户信息
    async getUserInfo({ commit }, token) {
        let result = await reqUserInfo(token)
        console.log(result)
        if(result.code==200) {
            commit("USERINFO", result.userInfo)
        }
    }
}
const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}